package com.example.administrator.yunniuusedcar.activity;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.administrator.common.base.BaseActivity;
import com.example.administrator.common.base.Global;
import com.example.administrator.yunniuusedcar.MainActivity;
import com.example.administrator.yunniuusedcar.R;
import com.example.administrator.yunniuusedcar.adapter.ArticleInfoAdapter;
import com.example.administrator.yunniuusedcar.fragment.MineFragment5Model;
import com.example.administrator.yunniuusedcar.model.mode_benn.IHttpService;
import com.example.administrator.yunniuusedcar.model.mode_benn.WZ_ArticleInfoBean;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 用户登录
 * Created by Administrator on 2017/12/12.
 */

public class UserLoginActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.LL_title)
    RelativeLayout LLTitle;
    @BindView(R.id.iv_loginPager)
    ImageView ivLoginPager;
    @BindView(R.id.et_world)
    EditText etWorld;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.btn_ZhuCe)
    Button btnZhuCe;
    @BindView(R.id.btn_WangJiPassword)
    Button btnWangJiPassword;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.iv_back, R.id.btn_login, R.id.btn_ZhuCe, R.id.btn_WangJiPassword, R.id.et_world, R.id.et_password})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;

  /////////
            case R.id.btn_login:
                //登陆
                if (!"".equals(etWorld.getText().toString()) && !"".equals(etPassword.getText().toString().trim())) {

                    String username = etWorld.getText().toString();
                    String password = etPassword.getText().toString();

                    Login(username, password);

                } else {
                    Toast.makeText(UserLoginActivity.this, "不可为空", Toast.LENGTH_SHORT).show();

                }
                ;
                break;


   ///////
            case R.id.btn_ZhuCe:  //注册
                startActivity(new Intent(UserLoginActivity.this, RegistrationActivity.class));
                break;
            case R.id.btn_WangJiPassword:  //忘记密码
                startActivity(new Intent(UserLoginActivity.this, ForgetPasswordActivity.class));
                break;
            default:
                break;
        }
    }

    //登陆
    private void Login(final String username, final String password) {    // 加载数据


        final String ul2 = IHttpService.HOST_URL + "Sign/getTocken";    // 获取sign值

        OkHttpUtils.post().url(ul2).addParams("tel_key", "33333333333333333").build().execute(new StringCallback() {     //.addParams("type","1")     222222222222222

            @Override
            public void onError(Call call, Exception e, int id) {
                Global.showToast("请求超时！" + e.getMessage());
                System.out.println("-----====----------A" + e);
            }

            @Override
            public void onResponse(String response, int id) {
                System.out.println("-----====----------A1" + response.toString());


                TOKEN mTOKEN = new Gson().fromJson(response, TOKEN.class);

                if (mTOKEN.getCode() == 1) {
                    String ul = IHttpService.HOST_URL + "member/login";    // 登录

                    OkHttpUtils.post().url(ul).addParams("sign", mTOKEN.getData()).addParams("phone", username).addParams("password", password).build().execute(new StringCallback() {     //.addParams("type","1")

                        @Override
                        public void onError(Call call, Exception e, int id) {
                            Global.showToast("请求超时！" + e.getMessage());
                            System.out.println("-----====----------A" + e);
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            System.out.println("-----====----------A" + response.toString());

                            UP MUP = new Gson().fromJson(response, UP.class);
                            if(MUP.getCode()==1){
                                //登陆成功，跳转
                                Intent intent = new Intent(UserLoginActivity.this, MainActivity.class);
                                intent.putExtra("fragid",5);
                                android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                                transaction.replace(R.id.view_pager , new MineFragment5Model());
                                startActivity(intent);

                                Toast.makeText(UserLoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                            }else {
                                //登录失败
                            }

                        }
                    });


//                WZ_ArticleInfoBean wzArticleInfoBean = new Gson().fromJson(response, WZ_ArticleInfoBean.class);
                }else{

                    //请求失败
                }
            }
        });


    }

}
